﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using RMS.Controller;

namespace RMS.View
{
    public partial class StudentResearchGroupView : UserControl
    {
        private StudentResearchGroupController _controller;

        public StudentResearchGroupView()
        {
            InitializeComponent();
        }

        public StudentResearchGroupController Controller
        {
            get { return this._controller; }
            set { this._controller = value; }
        }

        public string TextBoxGroupId { get { return this.textBoxGroupId.Text; } }
        public string ComboBoxAcademicGroup { get { return this.comboBoxAcademicProgram.Text; } }
        public string TextBoxGroupName { get { return this.textBoxGroupName.Text; } }
        public string TextBoxDescription { get { return this.textBoxGroupDescription.Text; } }

        private void buttonAddGroup_Click(object sender, EventArgs e)
        {
            this._controller.HandleAddGroupButtonClick();
        }

        private void buttonRemoveGroup_Click(object sender, EventArgs e)
        {
            if (this.listViewGroups.SelectedItems.Count > 0)
                this._controller.HandleRemoveGroupButtonClick();
        }

        private void buttonUpdateGroup_Click(object sender, EventArgs e)
        {
            this._controller.HandleUpdateGroupButtonClick();
        }

        private void buttonSearchGroup_Click(object sender, EventArgs e)
        {
            if (this.buttonSearchGroup.Text == "Search")
            {
                this.buttonSearchGroup.Text = "Cancel";
                this._controller.HandleSearcGroupClickButton();
            }
            else if (this.buttonSearchGroup.Text == "Cancel")
            {
                this.buttonSearchGroup.Text = "Search";
                this._controller.HandleCancelSearchGroupClickButton();
            }
        }

        private void listViewGroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listViewGroups.SelectedItems.Count <= 0) return;
            this.listViewGroupStudents.Items.Clear();
            ListViewItem item = this.listViewGroups.SelectedItems[0];
            this.textBoxGroupId.Text = item.SubItems[1].Text;
            this.comboBoxAcademicProgram.Text = item.SubItems[2].Text;
            this.textBoxGroupName.Text = item.SubItems[3].Text;
            this.textBoxGroupDescription.Text = item.SubItems[3].Text;
            this._controller.RefreshGroupStudentsList(Convert.ToInt32(this.textBoxGroupId.Text));
        }

        public void PopulateGroupList(List<Model.StudentResearchGroup> list)
        {
            this.listViewGroups.Items.Clear();
            foreach (Model.StudentResearchGroup group in list)
            {
                ListViewItem item = new ListViewItem();
                item.Text = group.Name;
                item.SubItems.Add(group.Id.ToString());
                item.SubItems.Add(group.ProgramId.ToString());
                item.SubItems.Add(group.Name);
                item.SubItems.Add(group.Description);
                this.listViewGroups.Items.Add(item);
            }
        }

        public void PopulateGroupStudentsList(List<Model.Student> list) {
            this.listViewGroupStudents.Items.Clear();
            foreach (Model.Student student in list)
            {
                ListViewItem item = new ListViewItem();
                item.Text = student.FirstName + " " + student.LastName;
                item.SubItems.Add(student.Id.ToString());
                this.listViewGroupStudents.Items.Add(item);
            }
        }

        public void PopulateStudentList(List<Model.Student> list)
        {
            this.listViewStudents.Items.Clear();
            foreach (Model.Student student in list)
            {
                ListViewItem item = new ListViewItem();
                item.Text = student.FirstName + " " + student.LastName;
                item.SubItems.Add(student.Id.ToString());
                this.listViewStudents.Items.Add(item);
            }
        }
    }
}
